<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>编辑用户</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="__LAYUI__/css/layui.css" rel="stylesheet">
    <style>
        .container {
            display: flex;
            justify-content: start;
        }

        .image-box {
            width: 100px;
            height: 100px;
            border: 2px dashed #ccc;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 2em;
            color: #ccc;
            cursor: pointer;
            position: relative;
            margin-right: 20px;
        }

        .image-box img {
            max-width: 100%;
            max-height: 100%;
        }

        .image-box .delete-btn {
            position: absolute;
            top: 0;
            right: 0;
            background-color: red;
            color: white;
            border: none;
            padding: 2px;
            cursor: pointer;
            display: none;
        }
    </style>
</head>

<body>
    <div class="layui-fluid layui-padding-3">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <label class="layui-form-label">QQ</label>
                <div class="layui-input-block">
                    <input type="text" name="qq" lay-verify="required" placeholder="请输入" autocomplete="off"
                        class="layui-input" value="{$data.qq}" disabled>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">密码</label>
                <div class="layui-input-block">
                    <input type="text" name="password" placeholder="请输入" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">昵称</label>
                <div class="layui-input-block">
                    <input type="text" name="nickname" lay-verify="required" placeholder="请输入" autocomplete="off"
                        class="layui-input" value="{$data.nickname}">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">用户状态</label>
                <div class="layui-input-inline">
                    <select name="status" lay-filter="aihao">
                        <option value="0" {$data.status==0 ? 'selected' : '' }>待审核</option>
                        <option value="1" {$data.status==1 ? 'selected' : '' }>正常</option>
                        <option value="2" {$data.status==2 ? 'selected' : '' }>封禁</option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">积分</label>
                    <div class="layui-input-inline" style="width: 100px;">
                        <input type="number" name="score" placeholder="" autocomplete="off" class="layui-input" min="0"
                            step="1" lay-affix="number" value="{$data.score}">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">经验</label>
                    <div class="layui-input-inline" style="width: 100px;">
                        <input type="number" name="exp" placeholder="" autocomplete="off" class="layui-input" min="0"
                            step="1" lay-affix="number" value="{$data.exp}">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">VIP时长</label>
                <div class="layui-input-block">
                    <input type="text" name="vip_time" placeholder="VIP时长" autocomplete="off"
                        class="layui-input" value="{$data.vip_time}" id="ID-laydate-shortcut-datetime">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">用户头像</label>
                <div class="container">
                    <div class="image-box" data-index="0">
                        {if @$data.avator != ''}
                        <img src="{$data.avator}" alt="图片1">
                        <button class="delete-btn" style="display: inline-block;">×</button>
                        {else}
                        +
                        {/if}
                    </div>
                </div>
                <input type="file" id="fileInput" style="display: none;">

            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button type="submit" class="layui-btn" lay-submit lay-filter="demo1">立即修改</button>
                </div>
            </div>
        </form>
    </div>


    <script src="__LAYUI__/layui.js"></script>
    <script>
        layui.use(function () {
            var form = layui.form;
            var layer = layui.layer;
            var laydate = layui.laydate;
            var util = layui.util;
            $ = layui.$;

            laydate.render({
                elem: "#ID-laydate-shortcut-datetime",
                type: "datetime",
                shortcuts: [
                    {
                        text: "昨天",
                        value: function () {
                            var now = new Date();
                            now.setDate(now.getDate() - 1);
                            return now;
                        }
                    },
                    {
                        text: "今天",
                        value: function () {
                            return Date.now();
                        }
                    },
                    {
                        text: "明天",
                        value: function () {
                            var now = new Date();
                            now.setDate(now.getDate() + 1);
                            return now;
                        }
                    },
                    {
                        text: "上个月",
                        value: function () {
                            var now = new Date();
                            var month = now.getMonth() - 1;
                            now.setMonth(month);
                            // 若上个月数不匹配，则表示天数溢出
                            if (now.getMonth() !== month) {
                                now.setDate(0); // 重置天数
                            }
                            return [now];
                        }
                    },
                    {
                        text: "下个月",
                        value: function () {
                            var now = new Date();
                            var month = now.getMonth() + 1;
                            now.setMonth(month);
                            // 若上个月数不匹配，则表示天数溢出
                            if (now.getMonth() !== month) {
                                now.setDate(0); // 重置天数
                            }
                            return [now];
                        }
                    },
                    {
                        text: "某一天",
                        value: "2016-10-14 10:00:00"
                    }
                ]
            });
            // 提交事件
            form.on('submit(demo1)', function (data) {
                var field = data.field; 
                field.id = "{$data.id}"
                img = $('.image-box').children('img').attr('src')
             
                field.avator = img
                $.post('/admin/user/doEdit', field, (res) => {
                    if (res.code == 200) {
                        layer.msg('修改成功')
                    } else {
                        layer.msg(res.msg)
                    }
                })
                return false;
            });


            // 上传图片
            const uploadUrl = '/admin/user/uploadAvator';
            images = ''
            $('.image-box').click(function () {
                th = $(this)
                if ($(this).text().trim() === '+') {
                    $('#fileInput').click();
                    $('#fileInput').off('change').on('change', function (event) {
                        const file = event.target.files[0];
                        if (file) {
                            const formData = new FormData();
                            formData.append('avator', file);

                            $.ajax({
                                url: uploadUrl,
                                type: 'POST',
                                data: formData,
                                contentType: false,
                                processData: false,
                                success: function (res) {
                                    if (res.code == 200 && res.data) {
                                        th.append('.image-box').html(`
                                    <img src="${res.data}" alt="Uploaded Image">
                                    <button class="delete-btn">&times;</button>
                                `);
                                        th.find('.delete-btn').show();
                                    } else {
                                        layer.msg(res.msg)
                                    }
                                },
                                error: function () {
                                    layer.msg('上传失败')
                                }
                            });
                        }
                    });
                }
            });

            $(document).on('click', '.delete-btn', function () {
                $(this).parent().html('+').find('.delete-btn').hide();
            });
        });


    </script>

    </script>
    </div>
</body>

</html>